package com.ocom.webapi;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.attribute.standard.PrinterURI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PrinterIPFetcher {
    public static void main(String[] args) {
        List<String> ipd = getIPs();
//        ipd.add("192.168.11.7");
//        System.out.println(ipd);
//        System.out.println(getHostnames(ipd));

        for (String ef : ipd){
            System.out.println(ef+"#是否打印机："+isPrinterIP(ef));
        }

    }


    public static boolean isPrinterIP(String ipAddress) {
        try {
            Socket socket = new Socket();
            socket.connect(new InetSocketAddress(ipAddress, 9100), 1000); // 9100是打印机的默认端口号
            socket.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static List<String>  getIPs()
    {
        List<String> list = new ArrayList<String>();
        boolean flag = false;
        int count=0;
        Runtime r = Runtime.getRuntime();
        Process p;
        try {
            p = r.exec("arp -a");
            BufferedReader br = new BufferedReader(new InputStreamReader(p
                    .getInputStream(), "GBK"));
            String inline;
            while ((inline = br.readLine()) != null) {
                if(inline.contains("接口")){
                    flag =true;
                }
                if(flag){
                    count++;
                    if(count > 2){
                        //有效IP
                        String[] str=inline.split(" {4}");
                        String ip = str[0];
                        ip= ip.replace(" ","");
                        list.add(ip);
                    }
                }
                System.out.println(inline);
            }
            br.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(list);
        return list;
    }

    public static void main5(String[] args) {
        {
            try {
                // 查找所有可用的打印服务
//                PrintService[] printServices = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
              PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);

                // 遍历打印服务
                for (PrintService printService : printServices) {
                    // 打印服务的名字可能包含打印机的IP地址
                    String serviceName = printService.getName();
                    System.out.println("Service Name: " + serviceName);

                    PrinterURI uri = (PrinterURI) printService.getAttribute(PrinterURI.class);
                    // 打印机的URI
                    if (uri != null) {
                        URI printerURI = new URI(uri.toString());
                        // 输出打印机的IP地址
                        System.out.println("打印机IP地址：" + printerURI.getHost());
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }



    public static void main4(String[] args) {

            List<String> list = new ArrayList<String>();
            boolean flag = false;
            int count = 0;
            Runtime r = Runtime.getRuntime();
            Process p;
            try {
                p = r.exec("ipconfig -all ");
                BufferedReader br = new BufferedReader(new InputStreamReader(p
                        .getInputStream(), "GBK"));
                String inline;
                while ((inline = br.readLine()) != null) {
                    if (inline.contains("192")) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                    if (flag) {
                        count++;
                        if (count > 2) {
                            //有效IP
                            String[] str = inline.split(" {4}");
                            String ip = str[0];
                            ip = ip.replace(" ", "");
                            list.add(ip);
                        }
                    }
                    System.out.println(inline);
                }
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(list);

    }


    public static Map<String,String> getHostnames(List<String> ips){

        Map<String,String> map = new HashMap<String,String>();
        System.out.println("正在提取hostname...");
        for(String ip : ips){
            String command = "ping -a " + ip;
            Runtime r = Runtime.getRuntime();
            Process p;
            try {
                p = r.exec(command);
                BufferedReader br = new BufferedReader(new InputStreamReader(p
                        .getInputStream()));
                String inline;
                while ((inline = br.readLine()) != null) {
                    if(inline.indexOf("[") > -1){
                        int start = inline.indexOf("Ping ");
                        int end = inline.indexOf("[");
                        String hostname = inline.substring(start+"Ping ".length(),end-1);
                        System.out.println(hostname);
                        map.put(ip,hostname);
                    }
                }
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("提取结束！ ");
        return map;
    }
}
